// import Api from '@/api';

export function objToParams(obj) {
    let str = '';
    for (const key of Object.keys(obj)) {
        if (str !== '') {
            str += '&';
        }
        if (obj[key]) {
            str += key + '=' + encodeURIComponent(obj[key]);
        }
    }
    return str;
}

export function paramsToObj(str) {
    let obj = {};
    try {
        obj = JSON.parse(
            '{"' +
            decodeURI(str)
            .replace(/"/g, '\\"')
            .replace(/&/g, '","')
            .replace(/=/g, '":"') +
            '"}'
        );
    } catch (e) {
        console.log(e);
    }
    return obj;
}

// export function getUrlParams(url) {
//     const params = url.split('?')[1];
//     return params ? paramsToObj(params) : {};
// }
/**
 * 秒转化成毫秒
 */
export function objSeToMs(obj) {
    const newObj = Object.assign({}, obj);
    for (const key in newObj) {
        if (key.includes('time') && key !== 'use_lesson_time_e') {
            newObj[key] *= 1000;
        }
    }
    return newObj;
}
/**
 * 毫秒转化成秒
 */
export function objMsToSe(obj) {
    const newObj = Object.assign({}, obj);
    for (const key in newObj) {
        if (key.includes('time') && key !== 'use_lesson_time_e') {
            newObj[key] = Math.round(newObj[key] / 1000);
        }
    }
    return newObj;
}

/**
 * 价格分转化元
 */
export function objFenToYuan(obj) {
    const newObj = Object.assign({}, obj);
    for (const key in newObj) {
        if (key.includes('price') || key.includes('coupon_val')) {
            newObj[key] = newObj[key] / 100;
        }
    }
    return newObj;
}
/**
 * 价格元转化分
 */
export function objYuanToFen(obj) {
    const newObj = Object.assign({}, obj);
    for (const key in newObj) {
        if (key.includes('price') || key.includes('coupon_val')) {
            newObj[key] = Math.round(newObj[key] * 100);
        }
    }
    return newObj;
}
/**
 * 数组转化成字符
 */
export function arrayToString(obj) {
    const newObj = Object.assign({}, obj);
    for (const key in newObj) {
        if (Array.isArray(newObj[key])) {
            newObj[key] = newObj[key].join();
        }
    }
    return newObj;
}
/**
 * 字符转化成数组
 */
export function stringToArray(obj) {
    return [obj];
}
/**
 *
 */
export function objToNumber(obj) {
    const newObj = Object.assign({}, obj);
    for (const key in newObj) {
        // && (key.includes('status') || key.includes('type')|| key.includes('style') || key.includes('is_hide') || key.includes('time') || key.includes('is_free'))
        if (newObj[key] !== null && newObj[key] !== '' && !isNaN(newObj[key]) && !key.match('[^s]*(?:id|student_nu|score_option|banner_val)[^s]*') && !Array.isArray(newObj[key])) {
            newObj[key] = +newObj[key];
        }
    }
    return newObj;
}

// export function coverReplace(url) {
//     // return url.replace(Api.baseUrl, '');
// }

export function stringToNumber(obj) {
    if (Array.isArray(obj)) {
        return obj.map((item) => {
            if (typeof item === 'object') {
                return objToNumber(item);
            } else {
                return item;
            }
        });
    } else if (typeof obj === 'object') {
        return objToNumber(obj);
    } else {
        return obj;
    }
}

export function getStatusStr(status, hide, stime, etime) {
    if (status === 5) {
        return '下架';
    } else if (+stime === 0) {
        return '暂不上架';
    } else if (+etime < new Date().getTime() / 1000) {
        return '已过期';
    } else if (+stime > new Date().getTime() / 1000) {
        return '等待上架';
    } else if (hide === 1) {
        return '隐藏上架';
    } else {
        return '上架';
    }
}

export function addAliupload(callback, onerror) {
    const proUpload = new Promise((resolve, reject) => {
        if (document.getElementById('proUpload')) {
            resolve();
            return true;
        }
        const sct = document.createElement('script');
        sct.id = 'proUpload';
        sct.type = 'text/javascript';
        sct.src = './aliyun-upload-sdk-1.5.0/aliyun-upload-sdk-1.5.0.min.js';
        document.getElementsByTagName('head')[0].appendChild(sct);
        sct.onload = resolve;
        sct.onerror = reject;
    });
    const proOss = new Promise((resolve, reject) => {
        if (document.getElementById('proOss')) {
            resolve();
            return true;
        }
        const sct = document.createElement('script');
        sct.id = 'proOss';
        sct.type = 'text/javascript';
        sct.src = `./aliyun-upload-sdk-1.5.0/lib/aliyun-oss-sdk-5.3.1.min.js`;
        document.getElementsByTagName('head')[0].appendChild(sct);
        sct.onload = resolve;
        sct.onerror = reject;
    });
    const proEs6 = new Promise((resolve, reject) => {
        if (document.getElementById('proEs6') || window.navigator.userAgent.indexOf('compatible') === -1 || window.navigator.userAgent.indexOf('MSIE') === -1) {
            resolve();
            return true;
        }
        const sct = document.createElement('script');
        sct.id = 'proEs6';
        sct.type = 'text/javascript';
        sct.src = `./aliyun-upload-sdk-1.5.0/lib/es6-promise.min.js`;
        document.getElementsByTagName('head')[0].appendChild(sct);
        sct.onload = resolve;
        sct.onerror = reject;
    });
    Promise.all([proUpload, proOss, proEs6])
        .then(() => {
            setTimeout(() => {
                callback();
            }, 500);
        })
        .catch((e) => {
            console.log(e);
            onerror();
        });
}